package com.sftz.modules.minapp.educationSign.module;

import java.io.IOException;
import java.io.InputStream;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import com.sftz.framework.constants.CommonConstants;
import com.sftz.framework.service.qiniu.QiniuDownloadService;
import com.sftz.modules.minapp.educationSign.form.RegistrationInformationUpdateForm;
import com.sftz.modules.minapp.educationSign.form.SignInfoAddForm;
import com.sftz.modules.minapp.educationSign.service.EducationSignViewService;
import com.sftz.modules.minapp.seriesCouresDetail.service.SerirsecoursedetailViewService;
import com.uxuexi.core.db.dao.impl.DbDao;

/**
 * 个人中心   学历注册
 * @author 郝振斌
 *
 * 2018年12月17日
 */
@IocBean
@At("/wx/educationSign")
public class EducationSignModule {

	private static final Log log = Logs.get();

	@Inject
	private EducationSignViewService educationSignViewService;
	
	@Inject
	private DbDao dbDao;
	
	@Inject
	private QiniuDownloadService qiniuUploadService;
	
	@Inject
	private SerirsecoursedetailViewService serirsecoursedetailViewService;

	/**
	 * 判断用户学历注册状态
	 */
	@At
	@Ok("json")
	public Object educationToList(@Param("token") final String token) {
		return educationSignViewService.list(token);
	}

	/**
	 * 查询学历注册列表的list
	 */
	@At
	@Ok("json")
	public Object educationListJson(@Param("token") final String token) {
		return educationSignViewService.list(token);
	}
	
	/**
	 * 查询学历注册列表的list
	 */
	@At
	@Ok("json")
	public Object enumList() {
		return educationSignViewService.getEducationEnumMap();
	}

	/**
	 * 添加
	 */
	@At
	public Object add(@Param("..") SignInfoAddForm addForm) {
		return educationSignViewService.addSignInfo(addForm);
	}

	/**
	 * 跳转到'修改操作'的录入数据页面,实际就是[按照主键查询单个实体]
	 */
	@At
	@Ok("json")
	public Object update(@Param("id") final long id) {
		return educationSignViewService.fetchValue(id);
	}

	/**
	 * 执行'修改操作'
	 */
	@At
	public Object updateSave(@Param("..") RegistrationInformationUpdateForm updateForm) {
		return educationSignViewService.updateSave(updateForm);
	}

	/**
	 * 审核结果页面
	 */
	@At
	@Ok("json")
	public Object educationCheck(@Param("id") final long id) {
		return educationSignViewService.checkResult(id);
	}

	/**
	 * 根据机构获取相关专业
	 * @param institutionId
	 * @return
	 */
	@At
	@Ok("json")
	public Object selectMajor(@Param("institutionId") final long institutionId) {
		return educationSignViewService.selectMajor(institutionId);
	}
	
	/**
	 * 	上传头像
	 * @throws IOException 
	 */
	@At
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:imgUpload" })
	public Object uploadImage(final @Param("file") TempFile file) throws IOException {
		//获取输入流
		InputStream inputStream = file.getInputStream();
		String fileName = file.getFile().getName();
		//文件后缀
		String suffix = Files.getSuffix(fileName);
		suffix = suffix.substring(1);
		String uploadImage = qiniuUploadService.uploadImage(inputStream, suffix, fileName);
		String imgPath = CommonConstants.IMAGES_SERVER_ADDR + uploadImage;
		return imgPath;
	}
}